#!/usr/bin/env Rscript

rm(list = ls())

setwd("../../data/voter_file_aggregated")

library(stargazer)

cumulative <- TRUE

## this plots output of model_dyad* files . it's model output instead of raw data.

load(paste("party_change_models_time2_lm_votes_together_coefsonly",ifelse(cumulative,"_cumulative",""),".RData",sep=""))
if (cumulative) {                       #just switched up the naming here by accident
load(paste("party_change_models_time2_lm_votes_together_coefsonly_placebo",ifelse(cumulative,"_cumulative",""),".RData",sep=""))
} else {
load("party_change_models_time2_lm_votes_together_placebo_coefsonly.RData")
}

plotTimeStuff <- function(coefs,ses,vcovs,plotName,diverge=TRUE,noAffilEtc=FALSE,axis.yes=FALSE,add=TRUE,the.pch=16,the.line.type=1,the.col="black",the.xs=1:4,the.ymax=7,the.cumulative=cumulative) {
    ##
    the.estimates <- names(coefs[[1]])
    the.ests <- grep("hid", the.estimates)
    ##
    intercept.ests <- sapply(
        coefs,
        function(x) x[1]
        )
    intercept.ses <- sapply(
        ses,
        function(x) x[1]
        )
    main.ests <- sapply(
        coefs,
        function(x) x[grep("hid",names(x))] + x[1]
        )
    ##
    main.ses <- sapply(
        ses,
        function(x) x[grep("hid",names(x))]
        )
    ##
    main.vcovs <- sapply(
        vcovs,
        function(x) x[2,1]
        )
    ##
    if (diverge & !add) {
    plot(the.xs, (main.ests[1,]), pch=8, ylim = c(-0.3, 0.05), bty="n", xaxt="n", cex=1.25, xlim=c(-1.5,4.5), ylab="Party Divergence", main = "",xlab="",type="n")
} else if (!add) {
        plot(the.xs, (main.ests[1,]), pch=8, ylim=c(-0.01,the.ymax), bty="n", xaxt="n", xlim=c(-1.5,4.5), ylab="Party Divergence", main = plotName,xlab="",cex=2,cex.lab=2,cex.main=1.8, cex.axis=2, yaxt=ifelse(axis.yes,"s","n"),type="n")
}
    ## lines(the.xs, c(main.ests[1,]), lwd = 2)
    abline(h = 1, lty = 2, col="red")
    abline(v = c(1.5), lty = 3, col="blue", lwd = 2)
    ## segments(the.xs, main.ests[1,] - 1.96 * main.ses[1,], the.xs, main.ests[1,] + 1.96 * main.ses[1,],lwd=3)
    if (diverge) {
    text(x=1.625,y=0.025,labels="separating",col="blue")
} else {
    ## text(x=1.625,y=0.25,labels="separating",col="blue")
}
    ##
    ##
    if (the.cumulative) {
    lines(the.xs, (main.ests[1,]),lwd=ifelse(!add,4,2),lty=the.line.type,col=the.col)
}
    points(the.xs, (main.ests[1,]), pch=the.pch, cex=ifelse(!add,2,1.75),col=the.col)
    segments(the.xs, main.ests[1,] - 1.96 * sqrt(main.ses[1,]^2 + intercept.ses^2 + 2*main.vcovs), the.xs, main.ests[1,] + 1.96 * sqrt(main.ses[1,]^2 + intercept.ses^2 + 2*main.vcovs),lwd=2)
}



## pdf(paste("dyad_separation",ifelse(cumulative,"_cumulative",""),".pdf",sep=""),height=7,width=7)
par(mfrow=c(1,1));  par(mar=c(0,0,7,1)); par(oma = c(5,8,0,0))
plotTimeStuff(
    DYAD.partyChangeModels.Switch.time2.lm.votes.coefs,
    DYAD.partyChangeModels.Switch.time2.lm.votes.ses,
    DYAD.partyChangeModels.Switch.time2.lm.votes.vcovs,
    the.ymax=0.05,
    the.xs=1:4,
    "8 year model\n(control first period)\n",diverge=FALSE,noAffilEtc=TRUE,axis.yes=TRUE,add=FALSE,the.pch=15,the.line.type=1,the.col="purple3")
plotTimeStuff(
    DYAD.partyChangeModels.Switch.time2.lm.votes.placebo.coefs,
    DYAD.partyChangeModels.Switch.time2.lm.votes.placebo.ses,
    DYAD.partyChangeModels.Switch.time2.lm.votes.placebo.vcovs,
    "",diverge=FALSE,noAffilEtc=TRUE,axis.yes=TRUE,add=TRUE,the.pch=15,the.line.type=1,the.col="gray",the.xs=-1:2-0.05
    )
mtext("separated -->",side=3,col="red",cex=1.8,at=2.25,padj=-1)
mtext(paste(ifelse(cumulative,"Cumulative","Marginal"),"Party Divergence"),side=2, padj = -3, cex = 2.25)
legend("topleft",legend=c("Switch,\nseparating:","08-10","12-14"), pch=c(NA,15,15),bty="n", cex = 1.8,col=c(NA,"purple3","gray"))
    axis(side=1, at = -1:2, labels = c("06-08","08-10", "10-12","12-14"), tick=FALSE,cex.axis=1.1,col.axis=gray(0.4),padj=-4)
    axis(side=1, at = c(1,3), labels = c("06-08", "10-12"), tick=FALSE,cex.axis=1.8,col.axis="purple3")
    axis(side=1, at = c(2,4), labels = c("08-10", "12-14"), tick=FALSE,cex.axis=1.8,padj=1.5,col.axis="purple3")
abline(h=0,lty=2,col="red")
text(x=3,y=0.01,labels="lower baseline:\n2012-2014 periods",col="black",cex=1.2)
text(x=3,y=0.001,labels="(comparison)",col="red",cex=1.2)
## dev.off()

## pdf(paste("dyad_separation2",ifelse(cumulative,"_cumulative",""),".pdf",sep=""),height=7,width=7)
par(mfrow=c(1,1));  par(mar=c(0,0,7,1)); par(oma = c(5,8,0,0))
plotTimeStuff(
    DYAD.partyChangeModels.Switch.time2.lm.votes.coefs,
    DYAD.partyChangeModels.Switch.time2.lm.votes.ses,
    DYAD.partyChangeModels.Switch.time2.lm.votes.vcovs,
    the.xs=1:4,
    "",diverge=FALSE,noAffilEtc=TRUE,axis.yes=TRUE,add=FALSE,the.pch=15,the.line.type=1,the.col="purple3",
    the.ymax=0.25)
plotTimeStuff(
    DYAD.partyChangeModels.Switch.time2.lm.votes.placebo.coefs,
    DYAD.partyChangeModels.Switch.time2.lm.votes.placebo.ses,
    DYAD.partyChangeModels.Switch.time2.lm.votes.placebo.vcovs,
    "",diverge=FALSE,noAffilEtc=TRUE,axis.yes=TRUE,add=TRUE,the.pch=15,the.line.type=1,the.col="gray",the.xs=-1:2-0.05
    )
plotTimeStuff(
    DYAD.partyChangeModels.DropAffil.time2.lm.votes.coefs,
    DYAD.partyChangeModels.DropAffil.time2.lm.votes.ses,
    DYAD.partyChangeModels.DropAffil.time2.lm.votes.vcovs,
    the.xs=1:4-0.025,
    "Abandon",diverge=FALSE,noAffilEtc=TRUE,axis.yes=TRUE,the.pch=8,the.line.type=2
    )
plotTimeStuff(
    DYAD.partyChangeModels.DropAffil.time2.lm.votes.placebo.coefs,
    DYAD.partyChangeModels.DropAffil.time2.lm.votes.placebo.ses,
    DYAD.partyChangeModels.DropAffil.time2.lm.votes.placebo.vcovs,
    the.xs=-1:2-0.05,
    "Abandon",diverge=FALSE,noAffilEtc=TRUE,axis.yes=TRUE,the.pch=8,the.line.type=2,the.col="gray"
    )
mtext("Cumulative Party Divergence",side=2, padj = -3, cex = 2.25)
legend("topleft",legend=c("Adopt","Abandon", "Switch,\nseparating:","08-10","12-14"), pch=c(17,8,NA,15,15),bty="n", cex = 1.8,col=c("black","black",NA,"black","gray"))
    axis(side=1, at = -1:2, labels = c("06-08","08-10", "10-12","12-14"), tick=FALSE,cex.axis=1.1,col.axis=gray(0.4),padj=-4)
    axis(side=1, at = c(1,3), labels = c("06-08", "10-12"), tick=FALSE,cex.axis=1.8,col.axis="purple3")
    axis(side=1, at = c(2,4), labels = c("08-10", "12-14"), tick=FALSE,cex.axis=1.8,padj=1.5,col.axis="purple3")
text(x=3,y=1.1,labels="(comparison)",col="red",cex=1.2)
plotTimeStuff(
    DYAD.partyChangeModels.noAffil.time2.lm.votes.coefs,
    DYAD.partyChangeModels.noAffil.time2.lm.votes.ses,
    DYAD.partyChangeModels.noAffil.time2.lm.votes.vcovs,
    the.xs=1:4+0.05,
    "Adopt",diverge=FALSE,noAffilEtc=TRUE,axis.yes=TRUE,the.pch=17,the.line.type=2,
    the.col="purple3"
    )
plotTimeStuff(
    DYAD.partyChangeModels.noAffil.time2.lm.votes.placebo.coefs,
    DYAD.partyChangeModels.noAffil.time2.lm.votes.placebo.ses,
    DYAD.partyChangeModels.noAffil.time2.lm.votes.placebo.vcovs,
    the.xs=-1:2-0.05,
    "Adopt",diverge=FALSE,noAffilEtc=TRUE,axis.yes=TRUE,the.pch=17,the.line.type=2,
    the.col="gray"
    )
## dev.off()
